"""
    Author:
    Date: 2019-12-19

    辅助函数。
"""

from django.conf import settings
from django.shortcuts import render
from entities.err import *
from entities import cpa_mgr
from functools import wraps
from utils import ajax
from utils import wx_util
from utils.log import Log

def jwt_required(view_func):
    """ 检测access token是否有效 """
    @wraps(view_func)
    def new_view_func(request, *args, **kwargs):
        access_token = request.META.get('HTTP_AUTHORIZATION', '')[7:]
        if access_token is None:
            return ajax.error(cpa_mgr.CPA_ERROR_INVALID_TOKEN)

        if not cpa_mgr.verify_access_token(access_token):
            return ajax.error(cpa_mgr.CPA_ERROR_INVALID_TOKEN)

        # Return the HttpResponse object
        response = view_func(request, *args, **kwargs)
        return response

    return new_view_func


# ------------------------------------------------------------------------------
if __name__ == '__main__':
    print('ok')
